package com.whisper.common.aspect;

import com.whisper.common.annotation.PageQuery;
import com.whisper.common.utils.PageUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
public class PageQueryAspect {
    
    @Pointcut("@annotation(com.whisper.common.annotation.PageQuery)")
    private void pageQuery(){};

    @Around("pageQuery()&&@annotation(page)")
    public Object startPage(ProceedingJoinPoint joinPoint, PageQuery page) throws Throwable {
        Class<?> returnType = ((MethodSignature) joinPoint.getSignature()).getMethod().getReturnType();
        if (!returnType.isAssignableFrom(List.class)) {
            throw new IllegalArgumentException("返回类型异常！");
        }
        if (!page.returnAll()) {
            PageUtils.startPage();
        }
        return joinPoint.proceed();
    }
}
